package org.mat.ipaas.ucenter.service.modules.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.mat.framework.lang.dto.MatApiResponse;
import org.mat.ipaas.ucenter.biz.common.system.query.QueryGenerator;
import org.mat.ipaas.ucenter.biz.modules.system.entity.SysFiles;
import org.mat.ipaas.ucenter.biz.modules.system.service.ISysFilesService;
import org.mat.ipaas.ucenter.service.api.system.SysFilesRestAPI;
import org.mat.ipaas.ucenter.service.dto.system.entity.SysFilesDTO;
import org.mat.ipaas.ucenter.service.modules.MatController;
import org.mat.ipaas.ucenter.service.modules.aop.AutoLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 知识库-文档管理
 */
@Slf4j
@Api(tags = "知识库-文档管理")
@RestController
@RequestMapping("/sys/files")
public class SysFilesController extends MatController<SysFiles, ISysFilesService> implements SysFilesRestAPI {

    @Autowired
    private ISysFilesService sysFilesService;

    /**
     * 分页列表查询
     *
     * @param sysFilesDTO
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "知识库-文档管理-分页列表查询")
    @ApiOperation(value = "知识库-文档管理-分页列表查询", notes = "知识库-文档管理-分页列表查询")
    @GetMapping(value = "/list")
    public MatApiResponse<?> queryPageList(SysFilesDTO sysFilesDTO,
                                           @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                           @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                           HttpServletRequest req) {
        SysFiles sysFiles = toSysFiles(sysFilesDTO);
        QueryWrapper<SysFiles> queryWrapper = QueryGenerator.initQueryWrapper(sysFiles, req.getParameterMap());
        Page<SysFiles> page = new Page<SysFiles>(pageNo, pageSize);
        IPage<SysFiles> pageList = sysFilesService.page(page, queryWrapper);
        return MatApiResponse.success(toSysFilesDTOPage(pageList));
    }

    /**
     * 添加
     *
     * @param sysFilesDTO
     * @return
     */
    @AutoLog(value = "知识库-文档管理-添加")
    @ApiOperation(value = "知识库-文档管理-添加", notes = "知识库-文档管理-添加")
    @PostMapping(value = "/add")
    public MatApiResponse<?> add(@RequestBody SysFilesDTO sysFilesDTO) {
        SysFiles sysFiles = toSysFiles(sysFilesDTO);
        sysFilesService.save(sysFiles);
        return MatApiResponse.success("添加成功！");
    }

    /**
     * 编辑
     *
     * @param sysFilesDTO
     * @return
     */
    @AutoLog(value = "知识库-文档管理-编辑")
    @ApiOperation(value = "知识库-文档管理-编辑", notes = "知识库-文档管理-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public MatApiResponse<?> edit(@RequestBody SysFilesDTO sysFilesDTO) {
        SysFiles sysFiles = toSysFiles(sysFilesDTO);
        sysFilesService.updateById(sysFiles);
        return MatApiResponse.success("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "知识库-文档管理-通过id删除")
    @ApiOperation(value = "知识库-文档管理-通过id删除", notes = "知识库-文档管理-通过id删除")
    @DeleteMapping(value = "/delete")
    public MatApiResponse<?> delete(@RequestParam(name = "id", required = true) String id) {
        sysFilesService.removeById(id);
        return MatApiResponse.success("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "知识库-文档管理-批量删除")
    @ApiOperation(value = "知识库-文档管理-批量删除", notes = "知识库-文档管理-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public MatApiResponse<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.sysFilesService.removeByIds(Arrays.asList(ids.split(",")));
        return MatApiResponse.success("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "知识库-文档管理-通过id查询")
    @ApiOperation(value = "知识库-文档管理-通过id查询", notes = "知识库-文档管理-通过id查询")
    @GetMapping(value = "/queryById")
    public MatApiResponse<?> queryById(@RequestParam(name = "id", required = true) String id) {
        SysFiles sysFiles = sysFilesService.getById(id);
        return MatApiResponse.success(sysFiles);
    }

    /**
     * 导出excel
     *
     * @param sysFilesDTO
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(SysFilesDTO sysFilesDTO) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();

        SysFiles sysFiles = toSysFiles(sysFilesDTO);
        return super.exportXls(request, sysFiles, SysFiles.class, "知识库-文档管理");
    }

    /**
     * 通过excel导入数据
     *
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public MatApiResponse<?> importExcel() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        HttpServletResponse response = attributes.getResponse();

        return super.importExcel(request, response, SysFiles.class);
    }


    private Page<SysFilesDTO> toSysFilesDTOPage(IPage<SysFiles> pageList) {
        Page<SysFilesDTO> sysFilesDTOPage = new Page<>();
        List<SysFilesDTO> sysFilesDTOList = new ArrayList<>();
        if (pageList != null && CollectionUtils.isNotEmpty(pageList.getRecords())) {
            for (SysFiles sysFiles : pageList.getRecords()) {
                sysFilesDTOList.add(toSysFilesDTO(sysFiles));
            }
        }

        sysFilesDTOPage.setCurrent(pageList.getCurrent());
        sysFilesDTOPage.setSize(pageList.getSize());
        sysFilesDTOPage.setTotal(pageList.getTotal());
        sysFilesDTOPage.setRecords(sysFilesDTOList);
        return sysFilesDTOPage;
    }

    private SysFilesDTO toSysFilesDTO(SysFiles sysFiles) {
        if (sysFiles == null) {
            return null;
        }
        SysFilesDTO sysFilesDTO = new SysFilesDTO();
        sysFilesDTO.setId(sysFiles.getId());
        sysFilesDTO.setFileName(sysFiles.getFileName());
        sysFilesDTO.setUrl(sysFiles.getUrl());
        sysFilesDTO.setCreateBy(sysFiles.getCreateBy());
        sysFilesDTO.setCreateTime(sysFiles.getCreateTime());
        sysFilesDTO.setUpdateBy(sysFiles.getUpdateBy());
        sysFilesDTO.setUpdateTime(sysFiles.getUpdateTime());
        sysFilesDTO.setFileType(sysFiles.getFileType());
        sysFilesDTO.setStoreType(sysFiles.getStoreType());
        sysFilesDTO.setParentId(sysFiles.getParentId());
        sysFilesDTO.setTenantId(sysFiles.getTenantId());
        sysFilesDTO.setFileSize(sysFiles.getFileSize());
        sysFilesDTO.setIzFolder(sysFiles.getIzFolder());
        sysFilesDTO.setIzRootFolder(sysFiles.getIzRootFolder());
        sysFilesDTO.setIzStar(sysFiles.getIzStar());
        sysFilesDTO.setDownCount(sysFiles.getDownCount());
        sysFilesDTO.setReadCount(sysFiles.getReadCount());
        sysFilesDTO.setShareUrl(sysFiles.getShareUrl());
        sysFilesDTO.setSharePerms(sysFiles.getSharePerms());
        sysFilesDTO.setEnableDown(sysFiles.getEnableDown());
        sysFilesDTO.setEnableUpdat(sysFiles.getEnableUpdat());
        sysFilesDTO.setDelFlag(sysFiles.getDelFlag());
        sysFilesDTO.setUserData(sysFiles.getUserData());
        sysFilesDTO.setRealname(sysFiles.getRealname());
        sysFilesDTO.setZipName(sysFiles.getZipName());
        return sysFilesDTO;
    }

    private SysFiles toSysFiles(SysFilesDTO sysFilesDTO) {
        if (sysFilesDTO == null) {
            return null;
        }
        SysFiles sysFiles = new SysFiles();
        sysFiles.setId(sysFilesDTO.getId());
        sysFiles.setFileName(sysFilesDTO.getFileName());
        sysFiles.setUrl(sysFilesDTO.getUrl());
        sysFiles.setCreateBy(sysFilesDTO.getCreateBy());
        sysFiles.setCreateTime(sysFilesDTO.getCreateTime());
        sysFiles.setUpdateBy(sysFilesDTO.getUpdateBy());
        sysFiles.setUpdateTime(sysFilesDTO.getUpdateTime());
        sysFiles.setFileType(sysFilesDTO.getFileType());
        sysFiles.setStoreType(sysFilesDTO.getStoreType());
        sysFiles.setParentId(sysFilesDTO.getParentId());
        sysFiles.setTenantId(sysFilesDTO.getTenantId());
        sysFiles.setFileSize(sysFilesDTO.getFileSize());
        sysFiles.setIzFolder(sysFilesDTO.getIzFolder());
        sysFiles.setIzRootFolder(sysFilesDTO.getIzRootFolder());
        sysFiles.setIzStar(sysFilesDTO.getIzStar());
        sysFiles.setDownCount(sysFilesDTO.getDownCount());
        sysFiles.setReadCount(sysFilesDTO.getReadCount());
        sysFiles.setShareUrl(sysFilesDTO.getShareUrl());
        sysFiles.setSharePerms(sysFilesDTO.getSharePerms());
        sysFiles.setEnableDown(sysFilesDTO.getEnableDown());
        sysFiles.setEnableUpdat(sysFilesDTO.getEnableUpdat());
        sysFiles.setDelFlag(sysFilesDTO.getDelFlag());
        sysFiles.setUserData(sysFilesDTO.getUserData());
        sysFiles.setRealname(sysFilesDTO.getRealname());
        sysFiles.setZipName(sysFilesDTO.getZipName());
        return sysFiles;
    }


}
